// router.js
import { createRouter, createWebHashHistory } from 'vue-router'
// 组件库
import { ElMessage } from 'element-plus'
import HomeViews from '../views/HomeViews'
import LoginViews from '../views/LoginViews'
import Cookies from 'js-cookie'

//路由部分
const routes = [
  {
    // path是路径
    path: '/',
    name: 'home',
    // 静态加载
    component: HomeViews,
    // 重定向到命名为 'workbench' 的路由。
    redirect: { name: 'workbench' },
    // 配置子路由
    children: [
      // 工作台
      {
        path: '/workbench',
        name: 'workbench',
        // 动态加载
        component: () => import('@/components/WorkbenchComponent.vue')
      },
      // 教师账号
      {
        path: '/teacher',
        name: 'teacher',
        component: () => import('@/components/TeacherComponent.vue')
      },
      // 学校管理
      {
        path: '/schoolmanagement',
        name: 'schoolmanagement',
        component: () => import('@/components/SchoolManagementComponent.vue')
      },
      // 学校管理员管理
      {
        path: '/schooladminmanagement',
        name: 'schooladminmanagement',
        component: () => import('@/components/SchoolAdminManagementComponent.vue')
      },
      // 学生账号
      {
        path: '/student',
        name: 'student',
        component: () => import('@/components/StudentComponent.vue')
      },
      // 就业管理
      {
        path: '/employment',
        name: 'employment',
        component: () => import('@/components/EmploymentComponent.vue')
      },
      // 个人中心
      {
        path: '/person',
        name: 'person',
        component: () => import('@/components/PersonComponent.vue')
      },
    ]
  },
  {
    path: '/Login',
    name: 'login',
    component: LoginViews,
    // 使用拦截的参数,是否有token,根据notoken来设置是否能进入该页面
    meta: {
      notoken: true
    },
  },
]

const router = createRouter({
  // 后期部署用createWebHistory
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.matched.some((r) => r.meta.notoken)) {
    next();
  } else {
    if (Cookies.get("token")) {
      next();
    } else {
      ElMessage({
        message: "请先登录",
        type: "warning"
      });
      next({
        path: '/login',
      });
    }
  }
});

export default router